﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.Marshalling.IIUnknownCacheStrategy;

namespace Cheems.Interface.SQL
{
    /// <summary>
    /// 数据库接口
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public interface IBaseRepository<TEntity> where TEntity : class
    {

        /// <summary>
        /// 实时数据数量
        /// </summary>
        int CurrentDataSize { get;}

        /// <summary>
        /// 更新连接字符串
        /// </summary>
        /// <param name="connectionString"></param>
        void UpdateConnectionString(string connectionString);

        /// <summary>
        /// 插入1条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="immediatelySave">是否立刻写入数据库</param>
        /// <returns></returns>
        Task<int> Insert(TEntity entity, bool immediatelySave = false);

        /// <summary>
        /// 插入多条数据
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        Task<int> Insert(List<TEntity> entities);

        /// <summary>
        /// 删除满足条件数据
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<int> Delete(Expression<Func<TEntity, bool>> whereExpression);

        /// <summary>
        /// 根据id更新单项
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<int> Update(TEntity entity);

        /// <summary>
        /// 更新满足条件的数据，只更新指定列的数据
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="updateColumns"></param>
        /// <returns></returns>
        Task<int> UpdateColumns(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TEntity>> updateColumns);


        /// <summary>
        /// 查询满足条件的数据
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression);
        Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression);

        /// <summary>
        /// 查询满足条件的数据，但限制返回数量
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int count);

        Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, int count);

        /// <summary>
        /// 按照条件和页码查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        Task<(List<TEntity>, int, int)> Query(Expression<Func<TEntity, bool>> whereExpression, int page, int size);

        /// <summary>
        /// 按照条件和页码异步查询
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        Task<(List<TEntity>, int, int)> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, int page, int size);

        /// <summary>
        /// 获取数据库表单
        /// </summary>
        /// <returns></returns>
        Task<List<string>> GetTableStringList();


        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="backupFilePath"></param>
        Task BackupDatabase(string backupFilePath);

        /// <summary>
        /// 恢复数据库
        /// </summary>
        /// <param name="backupFilePath"></param>
        /// <returns></returns>
        Task RecoveryDatabase(string backupFilePath);

    }
}
